import { HomePage } from "./components/HomePage/HomePage.js";
import { UserPage } from "./components/UserPage/UserPage.js";
import { WebElementsPage } from "./pages/WebElementsPage/WebElementsPage";
import { BaseLayout } from "./pages/Layout/base-layout/BaseLayout.js";
import { createRouter } from "./utils/router/index.js";
import { Register } from "./pages/Register/Register.js";
import { NotFound } from "./pages/NotFound/NotFound.js";
import { Login } from "./pages/Login/Login.js";
import { CompanyPage } from "./pages/CompanyPage/CompanyPage";
import { JobTypePage } from "./pages/JobTypePage/JobTypePage";
import { JobSeekerPage } from "./pages/JobSeekerPage/JobSeekerPage.js";
import { CompanyJobPage } from "./pages/CompanyJobPage/CompanyJobPage.js";
import { JobApplicationPage } from "./pages/JobApplicationPage/JobApplicationPage";
import { InterviewInvitationPage } from "./pages/InterviewInvitationPage/InterviewInvitationPage";

// 创建一个数据模型，包含用户信息和完整的菜单项
const data = {
  userName: "张三",
  userRole: "管理员",
  menuItems: [
    {
      type: "header",
      title: "Main",
    },
    {
      type: "dropdown",
      title: "首页",
      icon: "feather feather-monitor",

      items: [
        {
          title: "Home",
          href: "/Home",
        },
      ],
    },
    {
      type: "dropdown",
      title: "个人中心",
      icon: "feather feather-briefcase",
      items: [
        {
          title: "个人信息",
          href: "/User",
        },
        {
          title: "WebElementsPage",
          href: "/WebElementsPage",
        },
      ],
    },
    {
      type: "dropdown",
      title: "用户管理",
      icon: "feather feather-command",
      items: [
        {
          title: "User",
          href: "/User",
        },
        {
          title: "WebElementsPage",
          href: "/WebElementsPage",
        },
      ],
    },
    {
      type: "dropdown",
      title: "企业管理",
      icon: "feather feather-mail",
      items: [
        {
          title: "企业",
          href: "/companies",
        },
      ],
    },
    {
      type: "dropdown",
      title: "工作类型管理",
      icon: "feather feather-copy",
      items: [
        {
          title: "工作类型",
          href: "/job-types",
        },
      ],
    },
    {
      type: "dropdown",
      title: "企业招聘管理",
      icon: "fas fa-briefcase",
      items: [
        {
          title: "企业招聘",
          href: "/company-jobs",
        },
      ],
    },
    {
      type: "dropdown",
      title: "投简信息管理",
      icon: "feather feather-file",
      items: [
        {
          title: "投简信息",
          href: "job-application",
        },
      ],
    },
    {
      type: "dropdown",
      title: "面试邀请管理",
      icon: "feather feather-layout",
      items: [
        {
          title: "面试邀请",
          href: "interview-invitations",
        },
      ],
    },
    {
      type: "dropdown",
      title: "求职信息管理",
      icon: "feather feather-grid",
      items: [
        {
          title: "求职信息",
          href: "job-seekers",
        },
      ],
    },
    {
      type: "dropdown",
      title: "社区留言",
      icon: "feather feather-pie-chart",
      items: [
        {
          title: "社区留言",
          href: "chart-amchart.html",
        },
        {
          title: "apexchart",
          href: "chart-apexchart.html",
        },
      ],
    },
    {
      type: "dropdown",
      title: "系统管理",
      icon: "feather feather-feather",
      items: [
        {
          title: "Register",
          href: "/Register",
        },
        {
          title: "Material Design",
          href: "icon-material.html",
        },
      ],
    },
  ],
};
const footerData = {
  year: 2025,
  companyName: "Code Science",
  designer: "chengxiaokang",
  designerLink: "#",
};

const navbarData = {
  notifications: [
    {
      id: 1,
      type: "comment",
      title: "Datacorp",
      time: "1 min ago",
      message: "Caleb Flakelar commented on Admin",
      bgClass: "bg-primary",
      icon: "feather feather-mail",
      read: false,
    },
    {
      id: 2,
      type: "user1",
      title: "Admin",
      time: "1 hour ago",
      message: "New user registered",
      bgClass: "bg-info",
      icon: "feather feather-user-plus",
      read: true,
    },
    {
      id: 3,
      type: "user2",
      title: "Admin",
      time: "1 hour ago",
      message: "New user registered",
      bgClass: "bg-info",
      icon: "feather feather-user-plus",
      read: true,
    },
  ],
  unreadMessageCount: 2,
  unreadNotificationCount: 3,
};

// 路由配置
const router = createRouter({
  mode: "history",
  routes: {
    "/": () => renderPage(HomePage),
    "/Register": () => renderAppPage(Register),
    "/Login": () => renderAppPage(Login),
    "/Home": () => renderPage(HomePage),
    "/User": () => renderPage(UserPage),
    "/WebElementsPage": () => renderPage(WebElementsPage),
    "/companies": () => renderPage(CompanyPage),
    "/job-types": () => renderPage(JobTypePage),
    "/starter.html": () => renderPage(HomePage),
    "/job-seekers": () => renderPage(JobSeekerPage),
    "/company-jobs": () => renderPage(CompanyJobPage),
    "/job-application": () => renderPage(JobApplicationPage),
    "/interview-invitations": () => renderPage(InterviewInvitationPage),
    "*": () => renderAppPage(NotFound),
  },
});

function renderAppPage(PageClass) {
  const contentDOM = new PageClass().render();
  const appRoot = document.getElementById("app");
  appRoot.innerHTML = "";
  appRoot.appendChild(contentDOM);
}

// 页面渲染函数
function renderPage(PageClass) {
  const appContainer = document.getElementById("app");
  appContainer.innerHTML = "";
  const layout = new BaseLayout(data, footerData, navbarData);

  // 必须等待布局和页面的异步渲染完成
  layout
    .render(PageClass)
    .then((layoutDom) => {
      appContainer.appendChild(layoutDom);
    })
    .catch((error) => {
      console.error("页面渲染失败:", error);
    });
}

// 初始化逻辑
document.addEventListener("DOMContentLoaded", () => {
  // 隐藏加载动画
  const loader = document.querySelector(".loader");
  loader && (loader.style.display = "none");

  // 触发初始路由
  router.push(window.location.pathname);
});

// 暴露路由到全局
window.router = router;
